package com.dapeng.www.myapplication;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

/**
 * Created by Administrator on 2017/3/17.
 */
public class RadioButtonTableActivity extends FragmentActivity implements RadioGroup.OnCheckedChangeListener {



    private RadioGroup rg_tab_bar;
    private RadioButton rb_channel;
    private FragmentManager fragmentManager;


    private PrepareLessonFragement prepareLessonFragement;//导学界面
    private TeachingFragement teachingFragement;//授课界面
    private HomeWorkFragement homeWrokFragement;// 作业界面

    private FrameLayout fl_mainAcitivity_contain;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bottom_nvg_with_radio_button);
        fl_mainAcitivity_contain = (FrameLayout) findViewById(R.id.fl_mainAcitivity_contain);
        fragmentManager = getSupportFragmentManager();
        rg_tab_bar = (RadioGroup) findViewById(R.id.rg_tab_bar);
        rg_tab_bar.setOnCheckedChangeListener(this);
        //获取第一个单选按钮，并设置其为选中状态
        rb_channel = (RadioButton) findViewById(R.id.rb_channel);
        rb_channel.setChecked(true);

    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        selectCurrentFragement(checkedId);
    }


    /**
     * 定义选中当前fragement
     * @param checkedId
     */
    public void selectCurrentFragement(int checkedId) {
        //重置选项+隐藏所有Fragment
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragments(transaction);
        switch (checkedId) {
             case R.id.rb_channel:// 导学
                if (prepareLessonFragement == null) {
                    // 如果prepareLessonFragement为空，则创建一个并添加到界面上
                    prepareLessonFragement = new PrepareLessonFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, prepareLessonFragement);
                } else {
                    transaction.show(prepareLessonFragement);
                }
                break;

            case R.id.rb_message://授课
                if (teachingFragement == null) {
                    // 如果TeachingFragement为空，则创建一个并添加到界面上
                    teachingFragement = new TeachingFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, teachingFragement);
                } else {
                    transaction.show(teachingFragement);
                }
                break;
            case R.id.rb_better://作业
                if (homeWrokFragement == null) {
                    // 如果HomeWrokFragement为空，则创建一个并添加到界面上
                    homeWrokFragement = new HomeWorkFragement();
                    transaction.add(R.id.fl_mainAcitivity_contain, homeWrokFragement);
                } else {
                    transaction.show(homeWrokFragement);
                }
                break;
        }
        transaction.commit();
    }


    /**
     * 隐藏所有的Fragment,避免fragment混乱
     *
     * @param transaction
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (prepareLessonFragement != null) {
            transaction.hide(prepareLessonFragement);
        }
        if (teachingFragement != null) {
            transaction.hide(teachingFragement);
        }
        if (homeWrokFragement != null) {
            transaction.hide(homeWrokFragement);
        }
    }
}
